
<?php
    header('content-type:text/html;charset"utf-8"');

    // 定义统一的返回格式
    $responseData=array("code"=>0,"message"=>"");

    // 将前端传输到后端的数据全部加载在页面上
    $username=$_POST['username'];
    $passward=$_POST['passward'];
    $createTime=$_POST['createTime'];

    // 先给所有获取到的数据做一个简单的表单验证
    if(!$username){
        $responseData['code']=1;
        $responseData['message']="用户名不能为空";
        echo json_encode($responseData);
        exit;
    }
    if(!$passward){
        $responseData['code'] = 2;
        $responseData["message"] = "密码不能为空";
        //将数据按统一数据返回格式返回
        echo json_encode($responseData);
        exit;
    }

    //接着判断用户名是否已经被注册
    //天龙八部链接数据库
    $link = mysql_connect("127.0.0.1", "root", "123456");

    //判断数据库是否链接成功
    if(!$link){
        $responseData['code'] = 3;
        $responseData["message"] = "服务器忙";
        //将数据按统一数据返回格式返回
        echo json_encode($responseData);
        exit;
    }
    mysql_set_charset("utf8");

    mysql_select_db("huashuo");

    //准备sql验证是否已经被注册
	$sql = "SELECT * FROM account_number WHERE user='{$username}'";

	$res = mysql_query($sql);

    $row = mysql_fetch_assoc($res);

	if(!$row){
		/*
			密码要加密
		*/
		$str = md5(md5(md5($passward).'kuroneko').'kamineko');
		//准备sql语句进行注册
		$sql2 = "INSERT INTO account_number(user,passward,createTime) VALUES('{$username}','{$str}',{$createTime})";
       
        $res = mysql_query($sql2);
		if($res){
			$responseData['code'] = 6;
			$responseData['message'] = "注册成功,即将自动登录";
			echo json_encode($responseData);
		}else{
			$responseData['code'] = 4;
			$responseData['message'] = "注册失败";
			echo json_encode($responseData);
		}
	}else{
        $str = md5(md5(md5($passward).'kuroneko').'kamineko');
        $sql3 = "SELECT * FROM account_number WHERE passward='{$str}'";
        $res = mysql_query($sql3);
        $row = mysql_fetch_assoc($res);
        if(!$row){
            $responseData['code'] = 5;
            $responseData['message'] = "密码错误";
            echo json_encode($responseData);
            exit;
        }else{
            $responseData['code'] = 7;
            $responseData['message'] = "登录成功";
            echo json_encode($responseData);
            exit;
        }
	}

	mysql_close($link);

?>